import java.io.*;
import java.net.*;
import java.util.*;
import java.lang.*;

public class NS10_userA {
    
    public static int find_pow(int a, int b,int c) {
        int i,final_ans=1;
        for(i=0;i<b;i++)
            final_ans=(final_ans*(a%c))%c;
        return final_ans%c;
    }
    
    private static int findGCD(int number1, int number2) {

        if(number2 == 0){
            return number1;
        }
        return findGCD(number2, number1%number2);
    }

        

    public static int find_delta(int Xp,int Yp,int Xq,int Yq,int a,int P) {
    
        int delta;
        
        if(Xp==Xq&&Yp==Yq)
        {
            int number1=(3*(Xp*Xp)+a);
            int number2=(2*Yp);
            if(number1%number2!=0)
            {
                
                int gcd=findGCD(number1,number2);
                
                int i,temp=number2;
                number1=number1/gcd;
                temp=temp/gcd;
                
                for(i=0;i<P;i++)
                    if((temp*i)%P==1)
                        break;
                
                delta=((number1%P)*(i%P))%P;
            }
            else
            delta=((3*(Xp*Xp)+a)/(2*Yp))%P;
            
            
        }
        
        else
        {
            int number1=Yq-Yp;
            int number2=Xq-Xp;
            if(number1<0)
                number1=P+number1;
            if(number2<0)
                number2=P+number2;
            if(number1%number2!=0)
            {
                
                int gcd=findGCD(number1,number2);
                
                int i,temp=number2;
                number1=number1/gcd;
                temp=temp/gcd;
                
                
                for(i=0;i<P;i++)
                    if((temp*i)%P==1)
                    {
                        break;
                    }
                delta=((number1%P)*(i%P))%P;
            }
            else
            delta=(number1/number2)%P;
            
        }
        return delta;
    
    }
    
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter value of P");
        int P = sc.nextInt();
        
        System.out.println("Enter value of Ep");
        int a = sc.nextInt();
        int b = sc.nextInt();
        
        System.out.println("Enter value of G");
        int Xp = sc.nextInt();
        int Yp = sc.nextInt();

        int Xq=Xp;
        int Yq=Yp;
        
        System.out.println("Enter value of Na");
        int Na = sc.nextInt();

        int i,delta,j;
 
        for(i=0;Math.pow(2,i)<Na;i++);
        
        int Xr[],Yr[],Na_in_binary[],Xr_new[],Yr_new[];
        
        Xr=new int[i];
        Yr=new int[i];
        Xr_new=new int[i];
        Yr_new=new int[i];
        Na_in_binary=new int[i];
        Xr[0]=Xp;
        Yr[0]=Yp;
        for(j=1;j<=i-1;j++)
        {
        delta=find_delta(Xp,Yp,Xq,Yq,a,P);
        
        
        Xr[j]=((delta*delta)-Xp-Xq)%P;
        Yr[j]=(-1*Yp+delta*(Xp-Xr[j]))%P;
        
        if(Xr[j]<0)
            Xr[j]=P+Xr[j];
        
        if(Yr[j]<0)
            Yr[j]=P+Yr[j];
        
        
            Xp=Xq=Xr[j];
            Yp=Yq=Yr[j];
        }
        
        int Na_duplicate=Na;
        
        j=0;
        
        while(Na_duplicate!=0)
        {
        
            Na_in_binary[j]=Na_duplicate%2;
            Na_duplicate=Na_duplicate/2;
        
            j++;
        }
        j=0;
        for(j=0;j<i-1;)
        {
            int temp_num=j;
            j++;
            while(Na_in_binary[j]!=1&&j<i-1)
                j++;
            
            delta=find_delta(Xr[temp_num],Yr[temp_num],Xr[j],Yr[j],a,P);
           
            
            Xr_new[j]=((delta*delta)-Xr[temp_num]-Xr[j])%P;
            Yr_new[j]=((delta*(Xr[temp_num]-Xr_new[j]))-Yr[temp_num])%P;
            
            if(Xr_new[j]<0)
                Xr_new[j]=P+Xr_new[j];
            
            if(Yr_new[j]<0)
            {
                Yr_new[j]=P+Yr_new[j];
            }
            
            Xr[j]=Xr_new[j];
            Yr[j]=Yr_new[j];

        }
        
        
        for(j=0;j<i;j++);
        
        j--;
        ServerSocket ss = null;
        Socket s = null;
        ss = new ServerSocket(7777);
        s = ss.accept();
        DataOutputStream dos = new DataOutputStream(s.getOutputStream());
        DataInputStream dis = new DataInputStream(s.getInputStream());
        String from_userB = dis.readUTF();

        dos.writeUTF(Integer.toString(Xr_new[j])+","+Integer.toString(Yr_new[j]));
        
        String new_pa="",new_pb="";
        for(j=0;j<from_userB.length();j++)
            if(from_userB.charAt(j)!=',')
                new_pa+=from_userB.charAt(j);
        
            else
                break;
        
        
        for(j=j+1;j<from_userB.length();j++)
                new_pb+=from_userB.charAt(j);

        int new_point_A,new_point_B;
        
        new_point_A=Integer.parseInt(new_pa);
        new_point_B=Integer.parseInt(new_pb);

        
        Xp=Xq=new_point_A;
        Yp=Yq=new_point_B;
        
        Xr[0]=Xp;
        Yr[0]=Yp;
        for(j=1;j<=i-1;j++)
        {
            delta=find_delta(Xp,Yp,Xq,Yq,a,P);
            
            
            Xr[j]=((delta*delta)-Xp-Xq)%P;
            Yr[j]=(-1*Yp+delta*(Xp-Xr[j]))%P;
            
            if(Xr[j]<0)
                Xr[j]=P+Xr[j];
            
            if(Yr[j]<0)
                Yr[j]=P+Yr[j];
            
            
            Xp=Xq=Xr[j];
            Yp=Yq=Yr[j];
        }
        
         Na_duplicate=Na;
        
        j=0;
        
        while(Na_duplicate!=0)
        {
            
            Na_in_binary[j]=Na_duplicate%2;
            Na_duplicate=Na_duplicate/2;
            
            j++;
        }
        j=0;
        for(j=0;j<i-1;)
        {
            int temp_num=j;
            j++;
            while(Na_in_binary[j]!=1&&j<i-1)
                j++;
            
            delta=find_delta(Xr[temp_num],Yr[temp_num],Xr[j],Yr[j],a,P);
            
            
            Xr_new[j]=((delta*delta)-Xr[temp_num]-Xr[j])%P;
            Yr_new[j]=((delta*(Xr[temp_num]-Xr_new[j]))-Yr[temp_num])%P;
            
            if(Xr_new[j]<0)
                Xr_new[j]=P+Xr_new[j];
            
            if(Yr_new[j]<0)
            {
                Yr_new[j]=P+Yr_new[j];
            }
            
            Xr[j]=Xr_new[j];
            Yr[j]=Yr_new[j];
            
        }
        
        
        for(j=0;j<i;j++);
        j--;
        System.out.println("Key is : ("+Xr_new[j]+","+Yr_new[j]+")");
        
        
    }
}
